package com.findviewbydream.evil.eda.View.WebView;

import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import com.findviewbydream.evil.eda.R;


/**
 * Animator animator = ViewAnimationUtils.createCircularReveal(
 * layout,
 * layout.getWidth() / 2,
 * layout.getHeight() / 2,
 * 0,
 * (float) Math.hypot(layout.getWidth(), layout.getHeight()));
 * animator.setInterpolator(new AccelerateInterpolator());
 * animator.setDuration(1000);
 * animator.start();
 */

/********************************************
 * 工程名：Eda
 * 包名：com.findviewbydream.evil.eda
 * 时间：16-7-26 下午9:43
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：
 ********************************************/
public class Test extends AppCompatActivity
{
    private X5WebView webView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        initView();

        webView.loadUrl("https://www.baidu.com/");

        getWindow().setFormat(PixelFormat.TRANSLUCENT);

        webView.getView().setOverScrollMode(View.OVER_SCROLL_ALWAYS);
        webView.addJavascriptInterface(new WebViewJavaScriptFunction()
        {
            @Override
            public void onJsFunctionCalled(String tag)
            {

            }

            @JavascriptInterface
            public void onX5ButtonClicked()
            {
                Test.this.enableX5FullscreenFunc();
            }

            @JavascriptInterface
            public void onCustomButtonClicked()
            {
                Test.this.disableX5FullscreenFunc();
            }

            @JavascriptInterface
            public void onLiteWndButtonClicked()
            {
                Test.this.enableLiteWndFunc();
            }

            @JavascriptInterface
            public void onPageVideoClicked()
            {
                Test.this.enablePageVideoFunc();
            }
        }, "Android");


    }

    private void initView()
    {
        webView = (X5WebView) findViewById(R.id.webView);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig)
    {
        try
        {
            super.onConfigurationChanged(newConfig);
            if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
            {

            }
            else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
            {

            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }///////////////////////////////////////////

    //向webview发出信息
    private void enableX5FullscreenFunc()
    {
        Log.i("jsToAndroid", "enableX5FullscreenFunc happend!");

        if (webView.getX5WebViewExtension() != null)
        {
            Toast.makeText(this, "开启X5全屏播放模式", Toast.LENGTH_LONG).show();
            Bundle data = new Bundle();

            data.putBoolean("standardFullScreen", false);//true表示标准全屏，false表示X5全屏；不设置默认false，

            data.putBoolean("supportLiteWnd", false);//false：关闭小窗；true：开启小窗；不设置默认true，

            data.putInt("DefaultVideoScreen", 2);//1：以页面内开始播放，2：以全屏开始播放；不设置默认：1

            webView.getX5WebViewExtension().invokeMiscMethod("setVideoParams", data);
        }
    }

    private void disableX5FullscreenFunc()
    {
        Log.i("jsToAndroid", "disableX5FullscreenFunc happend!");
        if (webView.getX5WebViewExtension() != null)
        {
            Toast.makeText(this, "恢复webkit初始状态", Toast.LENGTH_LONG).show();
            Bundle data = new Bundle();

            data.putBoolean("standardFullScreen", true);//true表示标准全屏，会调起onShowCustomView()，false表示X5全屏；不设置默认false，

            data.putBoolean("supportLiteWnd", false);//false：关闭小窗；true：开启小窗；不设置默认true，

            data.putInt("DefaultVideoScreen", 2);//1：以页面内开始播放，2：以全屏开始播放；不设置默认：1

            webView.getX5WebViewExtension().invokeMiscMethod("setVideoParams", data);
        }
    }

    private void enableLiteWndFunc()
    {
        Log.i("jsToAndroid", "enableLiteWndFunc happend!");
        if (webView.getX5WebViewExtension() != null)
        {
            Toast.makeText(this, "开启小窗模式", Toast.LENGTH_LONG).show();
            Bundle data = new Bundle();

            data.putBoolean("standardFullScreen", false);//true表示标准全屏，会调起onShowCustomView()，false表示X5全屏；不设置默认false，

            data.putBoolean("supportLiteWnd", true);//false：关闭小窗；true：开启小窗；不设置默认true，

            data.putInt("DefaultVideoScreen", 2);//1：以页面内开始播放，2：以全屏开始播放；不设置默认：1

            webView.getX5WebViewExtension().invokeMiscMethod("setVideoParams", data);
        }
    }

    private void enablePageVideoFunc()
    {
        Log.i("jsToAndroid", "enablePageVideoFunc happend!");
        if (webView.getX5WebViewExtension() != null)
        {
            Toast.makeText(this, "页面内全屏播放模式", Toast.LENGTH_LONG).show();
            Bundle data = new Bundle();

            data.putBoolean("standardFullScreen", false);//true表示标准全屏，会调起onShowCustomView()，false表示X5全屏；不设置默认false，

            data.putBoolean("supportLiteWnd", false);//false：关闭小窗；true：开启小窗；不设置默认true，

            data.putInt("DefaultVideoScreen", 1);//1：以页面内开始播放，2：以全屏开始播放；不设置默认：1

            webView.getX5WebViewExtension().invokeMiscMethod("setVideoParams", data);
        }
    }
}

interface WebViewJavaScriptFunction
{
    void onJsFunctionCalled(String tag);
}